﻿/**
 * Created by Administrator on 2017/8/7.
 */
define(['jquery'],function ($) {
    var Paging = {
        name: 'page',
        template: ''
        + '<div class="c-page" >'
        + '    <div class="c-page-left left">显示 <span>1-{{pageRows}}</span> 条，共 <span>{{totalPages}}</span>页<span>{{totalRows}}</span> 条</div>'
        + ''
        + '    <div class="c-page-box">'
        + '        &nbsp;&nbsp;<Button type="primary" v-on:click.stop="elevator">确定</Button>'
        + '    </div>'
        + '    <div class="c-page-box">'
        + '        <Page v-bind:total="totalRows" v-bind:page-size="pageRows" show-elevator ref="iuPage" v-on:on-change="changePage"></Page>'
        + '    </div>'
        + '</div>',
        props: {
            pageRows: {
                type: Number,
                required: true
            },
        },
        data: function () {
            return {
                customerlist: [],
                //currentPage:1
                totalRows: 100,
                pageIndex:1
            }
        },
        computed: {
            totalPages: function () {
                var tp = Math.ceil(this.totalRows / this.pageRows);
                return tp;
            }
        },
        methods: {
            elevator: function () {
                var val = $(".ivu-page-options-elevator input").val();
                var page = 1;
                if (val > this.totalPages) {
                    page = this.totalPages;
                } else if (val <= 0) {
                    page = 1;
                } else {
                    page = val;
                }
                this.$refs.iuPage.changePage(parseInt(page));
                //this.currentPage=parseInt(page);
            },
            changePage: function (pageIndex) {
                this.pageIndex = pageIndex;
                //触发searchData
                window.bus.$emit("searchData", { pageNum: pageIndex, pageSize: this.pageRows });//通过 bus 触发数据查询事件.
            }
        },
        created: function () {
            var thisPage = this;
            //通过bus,监听数据总数,并跳回第一页事件，监听changePage
            window.bus.$on("changePage", function (data) {
                thisPage.totalRows = data.totalRows;
                var num = data.isBackFirstPage ? 1 : thisPage.pageIndex;
                thisPage.$refs.iuPage.changePage(num);
                //触发searchData
                window.bus.$emit("searchData", { pageNum: num, pageSize: thisPage.pageRows });//通过 bus 触发数据查询事件.
            })
        }
    };
    return Paging;
});
